const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const path=require('path');

// 创建 application/x-www-form-urlencoded 编码解析
app.use(bodyParser.urlencoded({ extended: false }));
// 创建 application/json 编码解析
app.use(bodyParser.json());

app.use('/images',express.static(path.join(__dirname,'images')));
app.use('/avator',express.static(path.join(__dirname,'avator')));

//设置跨域访问
app.use(require('cors')());
// app.all('*', function (req, res, next) {
//   res.header("Access-Control-Allow-Origin", "*")
//   res.header('Access-Control-Allow-Headers', 'Content-Type,Content-Length, Authorization,Origin,Accept,X-Requested-With');
//   res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS")
//   res.header("X-Powered-By", ' 3.2.1')
//   res.header("Content-Type", "application/json;charset=utf-8")
//   next()
// })

app.set('secret','abcd1xc5646');

app.use('/api/user',require('./routes/web/user'));
app.use('/api/image',require('./routes/web/image'));
//上面的接口是不需要登录就可以访问的，下面的则是需要登录、必须是管理员
require('./routes/admin/index')(app);
require('./routes/web/index')(app);

app.get('/', function(req, res, next){
    res.setHeader("Content-Type", "text/html;charset=utf-8");
    // res.sendFile(__dirname + "/client/index.html"  );
    // res.sendFile('C:/Users/abc/Desktop/jd-mobile/index.html');
});

// 错误处理函数
app.use(async (err, req, res, next) => {
  res.status(err.statusCode  || 500).json({
    statusCode:err.statusCode ,
    message: err.message
  })
})

let server = app.listen(2008, function() {
  console.log('image-bed-server is started~~~~~~~~~~~~~~~~~~~~~2008');
})

